<?php

namespace app\store\controller;

use library\Controller;
use library\tools\Express;
use think\Db;

/**
 * 订单记录管理
 * Class Order
 * @package app\store\controller
 */
class Order extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'StoreOrder';
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';            /*平台账户配置库*/
    protected $user_tradelog = 'tradelog';                                            /* 道具日志表 */
    protected function Db_connect_user_tradelog(){
        $user_tradelog= Db::connect($this->table_DB_Platform_Account_library)->table($this->user_tradelog);
        return $user_tradelog;
    }
    /**
     * 订单记录管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
            $request = input('request.');
            $list =$this->Db_connect_user_tradelog()
                 ->where($this->structure_where())
                 ->order('createdat desc')
                 ->paginate(20,false,['query'=>$request]);
            $data = $list->all();
            foreach ($data as $k => $v) {
                $channel_name = get_channel_name($v['channel_id']);
                $goods_name =get_Billing_name($v['goods_id']);
                $trade_name =get_pay_type($v['tradetype']);
                $v['channel_name'] = $channel_name;
                $v['server_name'] = server_name($v['extra']);
                $v['goods_name'] = $goods_name;
                $v['trade_name'] =  $trade_name;
                $list[$k] = $v;
            }
            $start_time = date('Y-m-d', time() - 2 * 24 * 3600) . ' - ' . date('Y-m-d', time()+24*3600);
            $pages = $list->render();
            /*渠道类型*/
            $channel_arr =Specific_parameters_Recharge_channel_arr();
            /*计费点类型*/
            $Billing_arr =Specific_parameters_commodities_arr();
            /*支付类型*/
            $pay_arr =Specific_parameters_commodities_pay_type_arr();
            $this->assign('Billing_arr',$Billing_arr);
            $this->assign('channel_arr',$channel_arr);
            $this->assign('pay_arr',$pay_arr);
            $this->assign('start_time', $start_time);
            $this->assign('list',$list);
            $this->assign('pages', $pages);
            $this->fetch();
    }
    protected function structure_where(){
        $pay_state    = $this->request->param('pay_state');
        $UserID    = $this->request->param('UserID');
        $order_no    = $this->request->param('order_no');
        $billing_type    = $this->request->param('billing_type');
        $pay_type    = $this->request->param('pay_type');
        $channel_type    = $this->request->param('channel_type');
        $pay_at    = $this->request->param('pay_at');
        $where=[];
        if(!empty($UserID)){
            $where[] =['userid','eq',$UserID];
        }
        if(!empty($order_no)){
            $where[] =['id','eq',$order_no];
        }
        if(!empty($billing_type)){
            $where[] =['goods_id','eq',$billing_type];
        }
        if(!empty($pay_type)){
            $where[] =['tradetype','eq',$pay_type];
        }
        if(!empty($pay_at)){
            $list = explode(' - ',$pay_at);
            $begin =$list[0];
            $after =$list[1];
            $where[] =['createdat','between',[$begin,$after.' 23:59:59']];
        }
        if(!empty($channel_type)){
            if($channel_type==30)$channel_type=0;
            $where[] =['channel_id','eq',$channel_type];
        }
        if(!empty($pay_state)){
            if($pay_state==2)$pay_state=0;
            $where[] =['status','eq',$pay_state];
        }
        return $where;
    }

    /**
     * 订单列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(array &$data)
    {
        $goodsList = Db::name('StoreOrderList')->whereIn('order_no', array_unique(array_column($data, 'order_no')))->select();
        $mids = array_unique(array_merge(array_column($data, 'mid'), array_column($data, 'from_mid')));
        $memberList = Db::name('StoreMember')->whereIn('id', $mids)->select();
        foreach ($data as &$vo) {
            list($vo['member'], $vo['from_member'], $vo['list']) = [[], [], []];
            foreach ($goodsList as $goods) if ($goods['order_no'] === $vo['order_no']) {
                $vo['list'][] = $goods;
            }
            foreach ($memberList as $member) if ($member['id'] === $vo['mid']) {
                $vo['member'] = $member;
            }
        }
    }

    /**
     * 修改快递管理
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function express()
    {
        if ($this->request->isGet()) {
            $where = ['is_deleted' => '0', 'status' => '1'];
            $this->expressList = Db::name('StoreExpress')->where($where)->order('sort desc,id desc')->select();
        }
        $this->_form($this->table);
    }

    /**
     * 快递追踪查询
     * @auth true
     */
    public function expressQuery()
    {
        list($code, $no) = [input('code', ''), input('no', '')];
        if (empty($no)) $this->error('快递编号不能为空！');
        if (empty($code)) $this->error('快递公司编码不能为空！');
        $this->result = Express::query($code, $no);
        $this->fetch();
    }

    /**
     * 快递表单处理
     * @param array $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _express_form_filter(&$vo)
    {
        if ($this->request->isPost()) {
            $order = Db::name($this->table)->where(['id' => $vo['id']])->find();
            if (empty($order)) $this->error('订单查询异常，请稍候再试！');
            $express = Db::name('StoreExpress')->where(['express_code' => $vo['express_company_code']])->find();
            if (empty($express)) $this->error('发货快递公司异常，请重新选择快递公司！');
            $vo['express_company_title'] = $express['express_title'];
            $vo['express_send_at'] = empty($order['express_send_at']) ? date('Y-m-d H:i:s') : $order['express_send_at'];
            $vo['express_state'] = '1';
            $vo['status'] = '4';
        }
    }

}
